<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class CI_Dynamic_assets {
	
	var $js=array();
	var $css=array();
	
	var $CI;
	
	function __construct(){
		$this->CI= &get_instance();
	}
	
	//para codigo js
	function add_code_js($code,$group='main') {
		$this->js[$group]['code'][]=$code;
	}
	
	function  add_unique_code_js($code){
		$this->js['main']['code'][md5($code)]=$code;
	}
	
	//para archivos .js
	function add_js($file,$group='main'){
		$this->js[$group]['file'][md5($file)]=$file;
	}
	
	//para codigo css
	function add_code_css($code,$group='main') {
		$this->css[$group]['code'][]=$code;
	}
	
	//para archivos .css
	function add_css($file,$group='main'){
		$this->css[$group]['file'][]=$file;
	}
	
	function display_js($group=null,$min=true){
		
		$this->display_js_files($group);
		
		if($min){
			
			$this->CI->load->helper('jsmin');
			echo minifyjs($this->display_js_code($group,true));
		} else {
			$this->display_js_code($group);
		}
	}
	
	function display_css($group=null,$min=true){
		
		$this->display_css_files($group);
		if($min){
			$this->CI->load->helper('cssmin');
			echo minifycss($this->display_css_code($group,true));
		} else {
			$this->display_css_code($group);
		}
		
	}	
	
	function display_js_code($group=null,$return=false){
		if($group){
			$jss['main']=&$this->js[$group];
		} else {
			$jss=&$this->js;
		}
		$scripts='';
		foreach ($jss as $group){
			if(isset($group['code'])){
				foreach ($group['code'] as $code){
					$scripts.=$code;
				}
			}
		}
		if($return){
			return $scripts;
		} else {
			echo $scripts;
		}
	}
	
	function display_js_files($group=null,$return=false){
		if($group){
			$jss['main']=&$this->js[$group];
		} else {
			$jss=&$this->js;
		}
		$links='';
		foreach ($jss as $group){
			if(isset($group['file'])){
				foreach ($group['file'] as $file){
					if($return){
						$files[]=$file;
					} else {
						$links.='<script src="'.$file.'"></script>';
					}
				}
			}
		}
		
		if($return){
			return $files;
		} else {
			echo $links;
		}
	}
	
	function display_css_code($group=null,$return=false){
		if($group){
			$css['main']=&$this->css[$group];
		} else {
			$css=&$this->css;
		}
		$csss='';
		foreach ($css as $group){
			if(isset($group['code'])){
				foreach ($group['code'] as $code){
					$csss.=$code;
				}
			}
		}
		if($return){
			return $csss;
		} else {
			echo $csss;
		}
	}
	
	function display_css_files($group=null,$return=false){
		if($group){
			$css['main']=&$this->css[$group];
		} else {
			$css=&$this->css;
		}
		$links='';
		foreach ($css as $group){
			if(isset($group['file'])){
				foreach ($group['file'] as $file){
					if($return){
						$files[]=$file;
					} else {
						$links.='<link rel="stylesheet" href="'.$file.'">';
					}
				}
			}
		}
		if($return){
			return $files;
		} else {
			echo $links;
		}
	}
}